import 'package:flutter/services.dart';

import 'number_text_input_formatter.dart';

//输入框输入限制
getInputFormat(keyboardType, {int? maxLength, double? maxSum}) {
//  debugPrint("限制里：$keyboardType");
//  debugPrint("限制里长度：$maxLength");

  ///避免TextInputType.number 和 TextInputType.numberWithOptions(decimal: true)互相干扰
  ///TextInputType.number 为限制输入最大值或最大长度
  ///TextInputType.numberWithOptions(decimal: true) 为限制两位小数
  if (keyboardType == TextInputType.number) {
    keyboardType = TextInputType.phone;
  }

  ///长度大小均无限制则默认无长度限制
  if (maxLength == null && maxSum == null) {
    maxLength = UsNumberTextInputFormatter.maxLength;
  }

  ///限制两位小数，限制长度
  if (keyboardType == const TextInputType.numberWithOptions(decimal: true) ||
      keyboardType == const TextInputType.numberWithOptions(decimal: false)) {
    return [
      UsNumberTextInputFormatter(maxSum: maxSum),
      FilteringTextInputFormatter.allow(RegExp(r'^[0-9]{1}([0-9]|[.])*$'))
    ];
  }

  ///限制输入数字，限制长度或大小
  if (keyboardType == TextInputType.phone) {
    return [
      maxLength != null
          ? LengthLimitingTextInputFormatter(maxLength)
          : UsNumberTextInputFormatter(maxSum: maxSum),
      FilteringTextInputFormatter.digitsOnly
    ];
  }

  ///无内容限制，只限制长度
  return [LengthLimitingTextInputFormatter(maxLength)];
}
